package com.century.sjt.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.util.Check;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

public class IdentityInfoActivity extends BaseActivity {
    private SjtTitleBar titleBar;
    private LinearLayout layout_TT;
    private EditText etName;
    private EditText etIdCard;
    private Button btnSave;
    private RequestQueue mQueue;
    private String idCard;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identity_info);
        mQueue = Volley.newRequestQueue(this);


        initView();
        initEvent();
    }
    @Override
    protected void initEvent() {
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_id_info));


    }
    @Override
    protected  void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.id_titlebar);
        etName = (EditText) findViewById(R.id.et_name);
        etIdCard = (EditText) findViewById(R.id.et_idcard);
        btnSave = (Button) findViewById(R.id.btn_save);
        layout_TT=(LinearLayout) findViewById(R.id.t_idtext);

    }

    @Override
    protected void onResume() {
        super.onResume();
        showRealName();

    }

    @Override
    protected void initData() {

    }

    //获取真实姓名
    private void showRealName(){
        //  获取头像实例
        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        idCard = mainSharedPreferences.getString("idCard", "");
        name = mainSharedPreferences.getString("name", "");
        String isRealName = mainSharedPreferences.getString("isRealName", "");
        if(isRealName.equals("1")){
            layout_TT.setVisibility(View.GONE);

            etName.setEnabled(false);
            etIdCard.setEnabled(false);
            etIdCard.setText(idCard.substring(0,4)+" ********* "+idCard.substring(idCard.length()-4,idCard.length()));

        }else {
            layout_TT.setVisibility(View.GONE);
            etName.setEnabled(false);
            etIdCard.setEnabled(false);
        }
        etName.setText(name);

//        etIdCard.setText(idCard);

    }

    public void saveRealNameBtn(View view) {
        name = etName.getText().toString();
        idCard = etIdCard.getText().toString();
        if(name.equals("")||name==null){
            TipUtil.showToast(getResources().getString(R.string.error_isno_name), IdentityInfoActivity.this, 1);
        }else if (!Check.isIdCard(idCard)){
            TipUtil.showToast(getResources().getString(R.string.error_isno_idcard), IdentityInfoActivity.this, 1);
        }else {
            saveRealName();
            //询问确认
//            AlertDialog.Builder builder = new AlertDialog.Builder(IdentityInfoActivity.this);
//            builder.setTitle("提示");
//            builder.setMessage("您还为设置支付密码，是否前去设置支付密码？");
//            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                    saveRealName();
//                }
//            });
//            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                }
//            });
//            builder.setCancelable(false);
//            builder.create().show();


        }
    }

    private void saveRealName() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), IdentityInfoActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SetRealNameHost, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("保存实名认证接口成功返回", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
                        TipUtil.showToast(message, IdentityInfoActivity.this, 1);
                        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = mainSharedPreferences.edit();
                        editor.putString("name", name);
                        editor.putString("idCard", idCard);
                        editor.putString("isRealName", "1");
                        editor.commit();
                        showRealName();//获取真实姓名
                        finish();
                    } else {//失败
                        TipUtil.showToast(message, IdentityInfoActivity.this, 1);
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), IdentityInfoActivity.this, 1);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), IdentityInfoActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");

                map.put("name", name);
                map.put("idCard", idCard);
                map.put("loginId", userId);
                return map;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
//                Log.e("cookie", cookieValue);
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }
}
